var express = require("express");
var router = express.Router();
var fs = require("fs")
var bodyParser = require("body-parser")

const {
  getList,
  getDetail,
  newBlog,
  updataBlog,
  delBlog,
  gettime,
  senddata,
  concern,
  getconcern,
  getsum,
  getlabel,
  getcontactlist,
  getDetails
} = require("../controller/blog");
const {
  SuccessModel,
  EeeorModel
} = require("../model/resModel");
const loginCheck = require("../middleware/loginCheck");

/* GET home page. */
router.get("/list", function (req, res, next) {
  const author = req.query.author || "";
  const keyword = req.query.keyword || "";
  const pagenunm = req.query.pagenunm || "";

  // const listData = getList(author,keyword)

  // return new SuccessModel(listData)
  console.log(req.query);

  if (req.query.isadmin) {
    if (req.session.username == null) {
      res.json(new EeeorModel("未登录"));
      return;
    }
    author = req.session.username;
  }

  const result = getList(author, keyword, pagenunm);

  return result.then(listData => {
   // console.log(listData);
    res.json(new SuccessModel(listData));
  });
});

router.get("/detail", function (req, res, next) {
  const data = getDetail(req.query.id);

  data.then(data => {
    res.json(new SuccessModel(data));
  });
});

router.get("/details", function (req, res, next) {
  const data = getDetails(req.query.id);

  data.then(data => {
    res.json(new SuccessModel(data));
  });
});


router.get("/time",function(req,res,next){

  gettime().then(data => {
       
          res.json(
    {time:data}
  )

  });

})

router.post("/new", loginCheck, (req, res, next) => {
  req.body.author = req.session.username;

  const result = newBlog(req.body);

  result.then(data => {
   // console.log(data);

    res.json(new SuccessModel(data));
  });
});

router.post("/update", loginCheck, (req, res, next) => {
  const result = updataBlog(req.body.id, req.body);

  return result.then(val => {
    if (val) {
      res.json(new SuccessModel("更新成功"));
    } else {
      res.json(new EeeorModel("更新失败"));
    }
  });
});

router.post("/del", loginCheck, (req, res, next) => {
  /* const author = req.session.username; */
  const author = "cxtao" ;
let id = req.body.id
  const result = delBlog(id, author);
  return result.then(val => {
    if (val) {
      res.json(new SuccessModel());
    } else {
      res.json(new EeeorModel("删除失败"));
    }
  });
});


router.post("/senddata",(req,res)=>{

     let {name,content,email} = req.body;
     let result  = senddata(name,content,email);
     result.then(data => {
     // console.log(data);
  
      res.json(new SuccessModel(data));
    });
})


router.post("/concern",(req,res)=>{

  let {name,email,blogLink,notes} = req.body;
  let result  = concern(name,email,blogLink,notes);
  result.then(data => {
 //  console.log(data);

   res.json(new SuccessModel(data));
 });
})

router.get("/getconcern",(req,res)=>{

  let result  =  getconcern();

  result.then(data => {
   // console.log(data);
 
    res.json(new SuccessModel(data));
  });

})

router.get("/getsum",(req,res)=>{

  let result = getsum();
return result.then((data)=>{

  res.json(new SuccessModel(data));
})

})

router.get("/getlabel",(req,res)=>{

  const name = req.query.name

  let result = getlabel(name)

  return result.then((data) =>{

    res.json(new SuccessModel(data))
  })
})

  router.get("/getlog",loginCheck,(req,res) =>{

fs.readFile("./logs/access.log","utf8",(err,data) =>{
  if(err){
    console.log(err)
    return
  }

 // console.log(data)
  res.send(data)
})

})

 router.post("/getcontactlist",(req,res) =>{

   const result = getcontactlist();

return result.then((data)=>{

  res.json(new SuccessModel(data));
})
 }
 )

module.exports = router;